# -*- coding: utf-8 -*-
# @Time    : 2024/9/2 15:33
# @Author  : floraachy
# @File    : repo_releases_page
# @Software: PyCharm
# @Desc:

# 标准库导入
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoReleasesPage(BasePage):
    """仓库发行版列表页"""

    # 【发布新版本】按钮
    locator_release_new_version_btn = "//span[text()='发布新版本']"
    # "标记"输入框
    locator_tag_input = "//div[text()='标记一个版本']/following-sibling::ul//input"
    # "分支/最近提交"下拉框
    locator_branch_or_commit_dropdown = "//span[text()='@']/following-sibling::div"
    # "最近提交"下拉值
    locator_branch_dropdown_value = "//a[text()='{}']"
    # "分支"下拉值
    locator_commit_dropdown_value = "//span[text()='{}']"
    # "名称"输入框
    locator_name_input = "//input[@id='name']"
    # "描述"输入框
    locator_desc_textarea = "//textarea/following::div[contains(@class, 'CodeMirror-wrap')]"
    # "这是一个预览版本"checkbox
    locator_release_version_checkbox = "//input[@type='checkbox']"
    # "文件"上传框
    locator_file_input = "//input[@type='file']"
    # 【创建发行版】按钮
    locator_new_release_btn = "//span[text()='创建发行版']/.."
    # 【保存发行版】按钮
    locator_save_release_btn = "//span[text()='保存发行版']/.."
    # 指定发行版的【编辑】图标
    locator_specified_release_edit_icon = "//div[@class='versionName']/a[text()='{}']/../span//i[contains(@class, 'icon-a-bianji1')]"
    # 指定发行版的【删除】图标
    locator_specified_release_delete_icon = "//div[@class='versionName']/a[text()='{}']/../span//i[contains(@class, 'icon-shanchuicon1 ')]"
    # 导航栏右侧弹窗提示语
    locator_tip_on_right_nav = "xpath=//div[@class='ant-notification-notice-description']"
    # 列表中所有发行版的标题
    locator_all_version_names = "//div[@class='versionName']/a"
    # 列表中所有发行版的附件
    locator_all_version_files = "//p[@class='versionFile']/a"
    # 列表中第一条发行版数据的标题
    locator_first_version_name = "(//div[@class='versionName'])[1]/a"
    # 列表中第一条发行版数据的描述
    locator_first_version_desc = "//div[@class='body-show']//p"
    # 列表中第一条发行版数据的文件
    locator_first_version_files = "(//p[@class='versionFile'])[1]/a"
    # 列表中第一条发行版数据的标签
    locator_first_version_tag = "(//span[@class='versionInfo_left'])[1]//a[contains(@href, 'tree')]"
    # 【是】按钮
    locator_yes_btn = "//span[text()='是']"

    @allure.step("访问仓库发行版列表页面：/{repo_owner}/{repo_identifier}/releases")
    def navigate(self, repo_owner, repo_identifier):
        """
        访问仓库发行版列表页面
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/releases")
        self.wait(1)

    @allure.step("点击【发布新版本】按钮")
    def click_release_new_version_button(self):
        """点击【发布新版本】按钮"""
        self.click(self.locator_release_new_version_btn)

    @allure.step("输入标记：{name}")
    def input_tag(self, name):
        """输入标记"""
        self.wait(1)
        self.input(self.locator_tag_input, name)

    @allure.step("输入发行版名称：{name}")
    def input_name(self, name):
        """输入发行版名称"""
        self.input(self.locator_name_input, name)

    @allure.step("输入发行版描述：{desc}")
    def input_desc(self, desc):
        """输入发行版描述"""
        # todo 疑惑：这里必须加wait_for_timeout，否则即使用的wait_for_selector，也无法输入内容。
        self.page.wait_for_timeout(2000)
        # elem = page.wait_for_selector(
        #     selector="//textarea/following::div[contains(@class, 'CodeMirror-wrap')]", timeout=10000, state='visible')
        elem = self.page.query_selector(selector=self.locator_desc_textarea)
        self.execute_js('''([elem, content]) => elem.CodeMirror.setValue(content);''',
                        [elem, desc])

    @allure.step("上传发行版附件：{files}")
    def upload_files(self, files):
        """上传发行版附"""
        if isinstance(files, list):
            for file in files:
                self.upload_file(self.locator_file_input, file)

        elif isinstance(files, str):
            self.upload_file(self.locator_file_input, files)

        else:
            raise TypeError("files参数类型错误，仅支持字符串或列表")

    @allure.step("勾选：这是一个预览版本")
    def check_pre_version(self):
        """勾选：这是一个预览版本"""
        self.check(self.locator_release_version_checkbox)

    @allure.step("点击【创建发行版】按钮")
    def click_create_version_button(self):
        """点击【创建发行版】按钮"""
        self.click(self.locator_new_release_btn)

    @allure.step("点击发行版：{name}的【编辑】按钮")
    def click_specific_version_edit_button(self, name):
        """点击指定发行版的【编辑】按钮"""
        self.click(self.locator_specified_release_edit_icon.format(name))
        self.wait(1)

    @allure.step("点击【保存发行版】按钮")
    def click_save_version_button(self):
        """点击【保存发行版】按钮"""
        self.click(self.locator_save_release_btn)

    @allure.step("点击发行版：{name}的【删除】按钮")
    def click_specific_version_delete_button(self, name):
        """点击指定发行版的【删除】按钮"""
        self.click(self.locator_specified_release_delete_icon.format(name))